// pages/login/login.js
const app = getApp()
var utils = require('../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isfalse: true,
        username:"",
        pwd:"",
        //判断小程序的API，回调，参数，组件等是否在当前版本可用。
        canIUse: wx.canIUse('button.open-type.getUserInfo')
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        // 查看是否授权
 


    },
    onShow:function(e){
        var username = wx.getStorageSync("login").username;
        if (username) {
            wx.reLaunch({
                url: '../index/index',
            })
        }
    },
    getUserInfo: function(e) {
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },

    //是否显示密码
    onloginyesTap: function(e) {
        var that = this;
        that.data.isfalse = !that.data.isfalse;
        that.setData({
            isfalse: that.data.isfalse
        })
    },
    userNameTap: function(e) {
        this.setData({
            username: e.detail.value
        })
    },
    userPwdTap: function(e) {
        this.setData({
            pwd: e.detail.value
        })
    },
    //formSubmit 登陆
    getUserInfo: function(e) {
        var that = this;
        if (that.data.username!= "" && that.data.pwd!=""){
            utils.http(httpUrl + "/mobile/login/index", {
                username: that.data.username,
                password: that.data.pwd
            }, function (res) {
                if (res.state == 1) {
                    wx.setStorageSync("key", res.datas.key);
                    app.globalData.userInfo = e.detail.userInfo;
                    if (e.detail.errMsg == "getUserInfo:fail auth deny") {
                        console.log("getUserInfo:fail auth deny")
                    } else {
                        that.setData({
                            userInfo: e.detail.userInfo,
                            hasUserInfo: true
                        })
                        wx.setStorageSync("login", {
                            username: that.data.username,
                            password: that.data.pwd
                        })
                        var user = { avatarUrl: res.datas.avatar, nickName: res.datas.nickname };
                            wx.setStorageSync("userInfo", user);
                        console.log(e.detail.userInfo.city);
                        utils.http(httpUrl + "/mobile/member/member_modify", {
                            address:e.detail.userInfo.city
                        }, function (res) {
                            console.log(res);
                        })
                        wx.reLaunch({
                            url: '../index/index',
                        })

                    }

                } else {
                    utils.message(res.datas);
                }
            })
        }else{
            if (that.data.username==""){
                wx.showToast({
                    title: '请输入用户名',
                    image:"/images/error.png"
                })
            } else if (that.data.pwd==""){
                wx.showToast({
                    title: '请输入密码',
                    image: "/images/error.png"
                })
            }
                
        }
  

    },

})